package com.cn.mmd.controller;



import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import com.cn.mmd.tools.SplitSpring;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 导入excel
 * Created by Administrator on 2016/6/17 0017.
 */
@Controller
@RequestMapping(value = "page/poi")
public class PoiImport {
    @Autowired
    private LogManageService logManageService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseWeeklyService courseWeeklyService;

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    private SplitSpring splitSpring = new SplitSpring();

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public String poi(@RequestParam("file") MultipartFile file, HttpServletRequest request
                     ,String o,Map f) {
        if (!file.isEmpty()) {
            try {
                o=request.getParameter("type").trim();
                String manageId = request.getParameter("manageId");
                String fileName = file.getOriginalFilename();
                String s = fileName.substring(fileName.lastIndexOf(".") + 1);
                System.out.println(s+","+o);
                // 文件保存路径
                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
                        + "yyyy."+s;
                // 转存文件
                file.transferTo(new File(filePath));
                if (s.equals("xlsx")) {
                   f = PoiImport.this.Xlsx(filePath,o);
                }else if (s.equals("xls")){
                    f=PoiImport.this.Xls(filePath,o);
                }else {
                    System.out.println("文件格式不对");
                }
                Date date = new Date();
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String logTime=time.format(date);
                LogManage logManage = new LogManage();
                logManage.setLogtime(logTime);
                logManage.setManageid(manageId);
                logManage.setLogcontent("导入了一张"+o+"表");
                logManageService.insertSelective(logManage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

            System.out.println(f.size());
        return "/s";
    }

    private Map Xls( String filePath,String o){
        HashMap<String,Object> map = new HashMap<String, Object>();
        HashMap<String,CourseWeekly> map1 = new HashMap<String, CourseWeekly>();
        try {
            HSSFWorkbook xwb = new HSSFWorkbook(new FileInputStream(filePath));
            HSSFSheet sheet = xwb.getSheetAt(0);
            int y;
            int rowNum;
            int cellNum;
            HSSFRow row;
            HSSFCell cell;
            //循环输出表格中的内容
            rowNum = sheet.getLastRowNum();
            row = sheet.getRow(0);
            y = row.getLastCellNum();
            if (o.trim().equals("teacher")) {
                Teacher s;
                Teacher teacher;
                String[] tea;
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                    tea = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }

                    teacher = new Teacher(tea[0], tea[1], tea[5], tea[2], tea[7], tea[3], tea[6]);
                     s = teacherService.selectByidRid(teacher.getTeacherid());
                    if (s == null) {
                        if (!teacher.getTeacherid().equals(""))
                            map.put(teacher.getTeacherid(), teacher);
                    }
                }
            }else if (o.equals("course")) {
                List<Teacher> teacher = new ArrayList<Teacher>();
                for (int i = 1; i <= rowNum; i++) {
                    String[] tea;
                    Course course;
                    Course c;
                    CourseWeekly courseWeekly;
                    String s;
                    CourseWeekly courseWeekly1;
                    int n;
                    int ji;
                    String s1;
                    String s2;
                    String c1;
                    String c2;
                    String c3;
                    int c4;
                    int c5;
                    String ss[];
                    String ss1[];
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                    tea = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }
//                    String [] tn = tea[8].split("/");
//                    String id = "";
//                    if (tn!=null&&tn.length>1) {
//                        for (int t = 0; t <tn.length;
//                        t++){
//                            teacher = teacherService.selectByName(tn[t]);
//                            if (teacher!=null&&teacher.size()>0){
//                                id =teacher.get(0).getTeacherid()+"/"+id;
//                            }
//                        }
//                    }else {
//                        teacher = teacherService.selectByName(tea[8]);
//                        if (teacher!=null&&teacher.size()>0) {
//                            id = teacher.get(0).getTeacherid();
//                        }
//                    }
                    course = new Course(tea[0], tea[1], tea[2], Float.valueOf(tea[3]), tea[4], tea[5], tea[6], tea[7],
                            tea[8], tea[10], tea[11], tea[12],tea[9]);
                    c = courseService.selectByKey(course.getCourseid());
                    if (c == null) {
                        if (!course.getCourseid().equals("")) {
                            map.put(course.getCourseid(), course);
                            course = new Course(course.getCourseclassroom(), course.getCoursetime(), course.getCourseid());
                            s1 = course.getCoursetime();
                            s2 = course.getCourseclassroom();
                            if (!s1.equals("") && !s2.equals("")) {
                                n = splitSpring.splitSpringBy(s1, 0, 0, ";", 1).length;
                                ji = splitSpring.splitSpringBy(s2, 0, 0, ";", 1).length;
                                if (n == 1 && ji > 1) {
                                    ss = splitSpring.splitSpringBy(s2, 0, 0, ";", n);
                                    for (int i1 = 0; i1 < ss.length; i1++) {
                                        if (ss[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(s1, 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(),(c4+j)+"" , c1, c2, ss[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }
                                    }
                                } else if (n > 1 && ji == 1) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    for (int i1 = 0; i1 < ss.length; i1++) {
                                        if (ss[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, s2);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }

                                    }
                                } else if (n > 1 && ji > 1 && n == ji) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    ss1 = splitSpring.splitSpringBy(s2, 0, 0, ";", ji);
                                    if (ss != null) {
                                        for (int i1 = 0; i1 < ss.length; i1++) {
                                            if (ss[i1] != null && ss1[i1] != null) {
                                                c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                                c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                                c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                                c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                                c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                                for (int j = 0 ;j < c5-c4+1;j++) {
                                                    courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                    courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                    s = i1+""+j+ course.getCourseid();
                                                    if (courseWeekly1 == null) {
                                                        map1.put(s, courseWeekly);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else if (n > 1 && ji > 1 && n != ji) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    ss1 = splitSpring.splitSpringBy(s2, 0, 0, ";", ji);
                                    int num = 0;
                                    if (n > ji) {
                                        num = n;
                                    } else {
                                        num = ji;
                                    }
                                    for (int i1 = 0; i1 < num; i1++) {
                                        if (ss[i1] != null && ss1[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        } else if (ss[i1] != null && ss1[i1] == null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[ji - 1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        } else if (ss[i1] == null && ss1[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[n - 1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[n - 1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }
                                    }
                                } else if (n == 1 && ji == 1) {
                                    c1 = splitSpring.splitSpringBy(s1, 0, 2, "", 1)[0];
                                    c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                    c3 = splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[2].split("周")[0];
                                    c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                    c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                    for (int j = 0 ;j < c5-c4+1;j++) {
                                        courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, s2);
                                        courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                        s = j+course.getCourseid();
                                        if (courseWeekly1 == null) {
                                            map1.put(s, courseWeekly);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (o.equals("student")) {
                System.out.println(new Date());
                String[] tea;
                Student student;
                Student s;
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                     tea = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }

                    student = new Student(tea[0], tea[1], tea[2], tea[3], tea[4], tea[5], tea[6],
                            tea[7], tea[8], tea[9], tea[10], tea[11]);
                     s = studentService.selectByKey(student.getStudentid());
                    if (s == null) {
                        if (!student.getStudentid().equals(""))
                            map.put(student.getStudentid(), student);
                    }
                }
            }System.out.println(new Date());
            Iterator iter = map.entrySet().iterator();
            if (o.equals("student")) {
                HashMap<String,Object> map2 = new HashMap<String, Object>();
                System.out.println(new Date());
                int k = 0;
                Map.Entry entry;
                StudentApp studentApp;
                Student student;
                String s;
                for (; k < map.size(); k++) {
                    entry= (Map.Entry) iter.next();
                    studentService.insertSelective((Student) entry.getValue());
                }Iterator iters = map.entrySet().iterator();
                for ( int k1 = 0  ; k1 < map.size() ; k1++ ){
                    entry= (Map.Entry) iters.next();
                    student = (Student) entry.getValue();
                    studentApp = new StudentApp();
                    studentApp.setStudentid(student.getStudentid());
                    if (student.getStudentid().length()>6){
                        s = student.getStudentid().substring(student.getStudentid().length()-6,student.getStudentid().length());
                        studentApp.setStudentpassword(DigestUtils.md5Hex(s));
                    }else {
                        s="123456";
                        studentApp.setStudentpassword(DigestUtils.md5Hex(s));
                    }
                    studentApp.setStudentname(student.getStudentname());
                    studentApp.setStudentsex(student.getStudentsex());
                    studentApp.setStudentphonenumber(student.getStudentphonenumber());
                    studentAppService.insertSelective(studentApp);
                    ShiroUserRole shiroUserRole = new ShiroUserRole();
                    shiroUserRole.setUserId(student.getStudentid());
                    shiroUserRole.setRoleId(1);
                    map2.put(student.getStudentid(), shiroUserRole);

                }
                Iterator iter2 = map2.entrySet().iterator();
                ShiroUserRole shiroUserRole;
                for ( int j = 0 ; j < map2.size() ; j++){
                     entry= (Map.Entry) iter2.next();
                   shiroUserRole = (ShiroUserRole) entry.getValue();
                    shiroUserRoleService.insertSelective(shiroUserRole);
                }
                System.out.println(new Date());
            }else  if (o.equals("teacher")) {
                int k = 0;
                Map.Entry entry;
                for (; k < map.size(); k++) {
                     entry = (Map.Entry) iter.next();
                    teacherService.insertSelective((Teacher) entry.getValue());
                }Iterator iters = map.entrySet().iterator();
                TeacherData teacherData;
                Teacher teacher;
                for (int k1 = 0 ; k1 < map.size(); k1++) {
                    entry=(Map.Entry) iters.next();
                    teacher = (Teacher) entry.getValue();
                    teacherData = new TeacherData();
                    teacherData.setTeacherid(teacher.getTeacherid());
                    teacherData.setTeachername(teacher.getTeachername());
                    teacherData.setTeacherpassword(DigestUtils.md5Hex(teacher.getTeacherid()));
                    teacherData.setTeachersex(teacher.getTeachersex());
                    teacherService.insertSelective(teacherData);
                    ShiroUserRole shiroUserRole = new ShiroUserRole();
                    shiroUserRole.setUserId(teacher.getTeacherid());
                    shiroUserRole.setRoleId(2);
                    shiroUserRoleService.insertSelective(shiroUserRole);
                }
            }else if (o.equals("course")){
                System.out.println(new Date());
                int k = 0;
                int k1= 0;
                CourseWeekly courseWeekly;
                Map.Entry entry ;
                System.out.println(map1.size()+"map1");
                for (; k < map.size(); k++) {
                    entry=(Map.Entry) iter.next();
                    courseService.insertSelective((Course) entry.getValue());
                } Iterator iter1 = map1.entrySet().iterator();
                for (;k1<map1.size();k1++){
                    entry= (Map.Entry) iter1.next();
                     courseWeekly = (CourseWeekly) entry.getValue();
                   courseWeeklyService.insertSelective(courseWeekly);
                }
                System.out.println(new Date());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            File file1 = new File(filePath);
            file1.delete();
        }
        return map;
    }




    private Map Xlsx(String filePath,String o) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        HashMap<String,CourseWeekly> map1 = new HashMap<String, CourseWeekly>();
        try {
            XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(filePath));
            //读取第一个sheet
            XSSFSheet sheet = xwb.getSheetAt(0);
            int y;
            //定义 row、cell
            XSSFRow row;
            XSSFCell cell;
            int rowNum;
            int cellNum;

            //循环输出表格中的内容
            rowNum = sheet.getLastRowNum();
            row = sheet.getRow(0);
            y = row.getLastCellNum();
            if (o.equals("teacher")) {
                System.out.println(new Date());
                Teacher teacher;
                Teacher s;
                String[] tea;
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                   tea  = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }

                    teacher = new Teacher(tea[0], tea[1], tea[5], tea[2], tea[7], tea[3], tea[6]);
                     s = teacherService.selectByidRid(teacher.getTeacherid());
                    if (s == null) {
                        if (!teacher.getTeacherid().equals(""))
                            map.put(teacher.getTeacherid(), teacher);
                    }
                }
            } else if (o.equals("course")){
                List<Teacher> teacher = new ArrayList<Teacher>();
                for (int i = 1; i <= rowNum; i++) {
                    String[] tea;
                    Course course;
                    Course c;
                    CourseWeekly courseWeekly;
                    String s;
                    CourseWeekly courseWeekly1;
                    int n;
                    int ji;
                    String s1;
                    String s2;
                    String c1;
                    String c2;
                    String c3;
                    int c4;
                    int c5;
                    String ss[];
                    String ss1[];
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                    tea = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }
//                    String [] tn = tea[8].split("/");
//                    String id = "";
//                    if (tn!=null&&tn.length>1) {
//                        for (int t = 0; t <tn.length;
//                             t++){
//                            teacher = teacherService.selectByName(tn[t]);
//                            if (teacher!=null&&teacher.size()>0){
//                                id =teacher.get(0).getTeacherid()+"/"+id;
//                            }
//                        }
//                    }else {
//                        teacher = teacherService.selectByName(tea[8]);
//                        if (teacher!=null&&teacher.size()>0) {
//                            id = teacher.get(0).getTeacherid();
//                        }
//                    }
                    course = new Course(tea[0], tea[1], tea[2], Float.valueOf(tea[3]), tea[4], tea[5], tea[6], tea[7],
                            tea[8], tea[10], tea[11], tea[12],tea[9]);
                    c = courseService.selectByKey(course.getCourseid());
                    if (c == null) {
                        if (!course.getCourseid().equals("")) {
                            map.put(course.getCourseid(), course);
                            course = new Course(course.getCourseclassroom(), course.getCoursetime(), course.getCourseid());
                            s1 = course.getCoursetime();
                            s2 = course.getCourseclassroom();
                            if (!s1.equals("") && !s2.equals("")) {
                                n = splitSpring.splitSpringBy(s1, 0, 0, ";", 1).length;
                                ji = splitSpring.splitSpringBy(s2, 0, 0, ";", 1).length;
                                if (n == 1 && ji > 1) {
                                    ss = splitSpring.splitSpringBy(s2, 0, 0, ";", n);
                                    for (int i1 = 0; i1 < ss.length; i1++) {
                                        if (ss[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(s1, 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(),(c4+j)+"" , c1, c2, ss[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }
                                    }
                                } else if (n > 1 && ji == 1) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    for (int i1 = 0; i1 < ss.length; i1++) {
                                        if (ss[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, s2);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }

                                    }
                                } else if (n > 1 && ji > 1 && n == ji) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    ss1 = splitSpring.splitSpringBy(s2, 0, 0, ";", ji);
                                    if (ss != null) {
                                        for (int i1 = 0; i1 < ss.length; i1++) {
                                            if (ss[i1] != null && ss1[i1] != null) {
                                                c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                                c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                                c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                                c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                                c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                                for (int j = 0 ;j < c5-c4+1;j++) {
                                                    courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                    courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                    s = i1+""+j+ course.getCourseid();
                                                    if (courseWeekly1 == null) {
                                                        map1.put(s, courseWeekly);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else if (n > 1 && ji > 1 && n != ji) {
                                    ss = splitSpring.splitSpringBy(s1, 0, 0, ";", n);
                                    ss1 = splitSpring.splitSpringBy(s2, 0, 0, ";", ji);
                                    int num = 0;
                                    if (n > ji) {
                                        num = n;
                                    } else {
                                        num = ji;
                                    }
                                    for (int i1 = 0; i1 < num; i1++) {
                                        if (ss[i1] != null && ss1[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        } else if (ss[i1] != null && ss1[i1] == null) {
                                            c1 = splitSpring.splitSpringBy(ss[i1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[ji - 1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        } else if (ss[i1] == null && ss1[i1] != null) {
                                            c1 = splitSpring.splitSpringBy(ss[n - 1], 0, 2, "", 1)[0];
                                            c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(ss[n - 1], 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                            c3 = splitSpring.splitSpringBy(ss[i1], 0, 0, "第", 3)[2].split("周")[0];
                                            c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                            c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                            for (int j = 0 ;j < c5-c4+1;j++) {
                                                courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, ss1[i1]);
                                                courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                                s = i1+""+j+ course.getCourseid();
                                                if (courseWeekly1 == null) {
                                                    map1.put(s, courseWeekly);
                                                }
                                            }
                                        }
                                    }
                                } else if (n == 1 && ji == 1) {
                                    c1 = splitSpring.splitSpringBy(s1, 0, 2, "", 1)[0];
                                    c2 = splitSpring.splitSpringBy(splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[1], 0, 0, "节", 2)[0];
                                    c3 = splitSpring.splitSpringBy(s1, 0, 0, "第", 3)[2].split("周")[0];
                                    c4 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[0]);
                                    c5 = Integer.parseInt(splitSpring.splitSpringBy(c3, 0, 0, "-", 2)[1]);
                                    for (int j = 0 ;j < c5-c4+1;j++) {
                                        courseWeekly = new CourseWeekly(course.getCourseid(), (c4+j)+"", c1, c2, s2);
                                        courseWeekly1 = courseWeeklyService.selectByAll(courseWeekly);
                                        s = j+course.getCourseid();
                                        if (courseWeekly1 == null) {
                                            map1.put(s, courseWeekly);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (o.equals("student")) {
                System.out.println(new Date());
                Student student;
                Student s;
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    cellNum = row.getLastCellNum();
                    if (cellNum != y) {
                        cellNum = y;
                    }
                    String[] tea = new String[cellNum];
                    for (int j = 0; j < cellNum; j++) {
                        cell = row.getCell(j);
                        if (row.getCell(j) != null) {
                            row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            tea[j] = cell.getStringCellValue().trim();
                        } else {
                            tea[j] = "";
                        }
                    }

                    student = new Student(tea[0], tea[1], tea[2], tea[3], tea[4], tea[5], tea[6],
                            tea[7], tea[8], tea[9], tea[10], tea[11]);
                     s = studentService.selectByKey(student.getStudentid());
                    if (s == null) {
                        if (!student.getStudentid().equals(""))
                            map.put(student.getStudentid(), student);
                    }
                }
            }
            System.out.println(new Date());
            Iterator iter = map.entrySet().iterator();
            Iterator iter1 = map1.entrySet().iterator();
            if (o.equals("student")) {
                HashMap<String,Object> map2 = new HashMap<String, Object>();
                System.out.println(new Date());
                int k = 0;
                Map.Entry entry ;
                StudentApp studentApp;
                Student student;
                String s;
                for (; k < map.size(); k++) {
                    entry= (Map.Entry) iter.next();
                    studentService.insertSelective((Student) entry.getValue());
                } Iterator iters = map.entrySet().iterator();
                for ( int k1 = 0  ; k1 < map.size() ; k1++ ){
                    entry= (Map.Entry) iters.next();
                    student = (Student) entry.getValue();
                    studentApp = new StudentApp();
                    studentApp.setStudentid(student.getStudentid());
                    if (student.getStudentid().length()>6){
                        s = student.getStudentid().substring(student.getStudentid().length()-6,student.getStudentid().length());
                        studentApp.setStudentpassword(DigestUtils.md5Hex(s));
                    }else {
                        s="123456";
                        studentApp.setStudentpassword(DigestUtils.md5Hex(s));
                    }
                    studentApp.setStudentname(student.getStudentname());
                    studentApp.setStudentsex(student.getStudentsex());
                    studentApp.setStudentphonenumber(student.getStudentphonenumber());
                    studentAppService.insertSelective(studentApp);
                    ShiroUserRole shiroUserRole = new ShiroUserRole();
                    shiroUserRole.setUserId(student.getStudentid());
                    shiroUserRole.setRoleId(1);
                    map2.put(student.getStudentid(),shiroUserRole);
                } Iterator iter2 = map2.entrySet().iterator();
                ShiroUserRole shiroUserRole;
                for ( int j = 0 ; j < map2.size() ; j++){
                    entry= (Map.Entry) iter2.next();
                    shiroUserRole = (ShiroUserRole) entry.getValue();
                    shiroUserRoleService.insertSelective(shiroUserRole);
                }
                System.out.println(new Date());
            } else if (o.equals("teacher")) {
                System.out.println(new Date());
                int k = 0;
                Map.Entry entry ;
                Teacher teacher;
                for (; k < map.size(); k++) {
                    entry=(Map.Entry) iter.next();
                    teacherService.insertSelective((Teacher) entry.getValue());
                }Iterator iters = map.entrySet().iterator();
                TeacherData teacherData;
                for (int k1 = 0 ; k1 < map.size(); k1++) {
                    entry=(Map.Entry) iters.next();
                    teacher = (Teacher) entry.getValue();
                    teacherData = new TeacherData();
                    teacherData.setTeacherid(teacher.getTeacherid());
                    teacherData.setTeachername(teacher.getTeachername());
                    teacherData.setTeacherpassword(DigestUtils.md5Hex(teacher.getTeacherid()));
                    teacherData.setTeachersex(teacher.getTeachersex());
                    teacherService.insertSelective(teacherData);
                    ShiroUserRole shiroUserRole = new ShiroUserRole();
                    shiroUserRole.setUserId(teacher.getTeacherid());
                    shiroUserRole.setRoleId(2);
                    shiroUserRoleService.insertSelective(shiroUserRole);
                }
                System.out.println(new Date());
            }else if (o.equals("course")){
                System.out.println(new Date());
                int k = 0;
                int k1= 0;
                CourseWeekly courseWeekly;
                Map.Entry entry ;
                System.out.println(map1.size()+"map1");
                for (; k < map.size(); k++) {
                    entry=(Map.Entry) iter.next();
                    courseService.insertSelective((Course) entry.getValue());
                }for (;k1<map1.size();k1++){
                    entry= (Map.Entry) iter1.next();
                    courseWeekly = (CourseWeekly) entry.getValue();
                    courseWeeklyService.insertSelective(courseWeekly);
                }
                System.out.println(new Date());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            File file1 = new File(filePath);
            file1.delete();
        }
        return map;
    }
}